"""
Case Type   : bugfix
Case Name   : 创建package，package里函数的schema和package所在schema不一致，合理报错
Create At   : 2025/6/12
Owner       : liu-tong-8848
Description :
    1、创建schema
    2、创建package，package里函数的schema和package所在schema不一致
    3、清理环境
Expect      :
    1、成功
    2、合理报错
    3、成功
History     :
"""

import os
import unittest

from yat.test import Node
from yat.test import macro

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class BugFix(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.common = Common()
        self.sh_primary = CommonSH('PrimaryDbUser')
        self.user_node = Node('PrimaryDbUser')
        self.constant = Constant()

    def test_dolphin(self):
        self.log.info(f'----{os.path.basename(__file__)} start----')
        text = "----step1:创建schema； expect:成功----"
        self.log.info(text)
        sql_cmd = "drop schema if exists s_fixed_111; create schema s_fixed_111;"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn(self.constant.DROP_SCHEMA_SUCCESS_MSG, sql_res, '执行失败' + text)
        self.assertIn(self.constant.CREATE_SCHEMA_SUCCESS_MSG, sql_res, '执行失败' + text)

        text = '----step2:创建package，package里函数的schema和package所在schema不一致；expect:合理报错----'
        self.log.info(text)
        sql_cmd = "drop package if exists p_fixed_111; " \
                  + "create or replace package p_fixed_111 is " \
                  + "var1 int:=1; var2 int:=2; procedure s_fixed_111.testpro1(var3 int); " \
                  + "end p_fixed_111;"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn(self.constant.drop_package_success_msg, sql_res, '执行失败' + text)
        self.assertIn('ERROR', sql_res, '执行失败' + text)
        self.assertIn('The namespace of functions or procedures within a package needs to '
                      + 'be consistent with the package it belongs', sql_res, '执行失败' + text)

    def tearDown(self):
        text = '----step3:清理环境；expect:成功----'
        self.log.info(text)
        sql_cmd = f"drop schema if exists s_fixed_111; drop package if exists p_fixed_111;"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn(self.constant.DROP_SCHEMA_SUCCESS_MSG, sql_res, '执行失败' + text)
        self.assertIn(self.constant.drop_package_success_msg, sql_res, '执行失败' + text)
        self.log.info(f'----{os.path.basename(__file__)} end-----')
